
<template>
    <div class="user">
        <!-- 面包屑 -->
        <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>权限管理</el-breadcrumb-item>
            <el-breadcrumb-item>角色列表</el-breadcrumb-item>
        </el-breadcrumb>
        <!-- 搜索 -->
        <div style="margin-top: 15px;">
            <el-button type="success" plain @click='addDialogFormVisible=true'>添加角色</el-button>
        </div>
        <!-- 表格 -->
        <el-table :data="rolesList" border style="width: 100% ;margin-top: 15px;">
            <el-table-column type="expand">
                <template slot-scope="scope">
                    <el-row v-for="v1 in scope.row.children" :key="v1.id">
                        <el-col :span="4">
                            <el-tag closable type="success" @close="delAccreditV1(scope.row,v1.id)">
                                {{v1.authName}}
                            </el-tag>
                        </el-col>
                        <el-col :span="20">
                            <el-row v-for="v2 in v1.children" :key="v2.id">
                                <el-col :span="4">
                                    <el-tag closable type="error" @close="delAccreditV2(scope.row,v2.id)">
                                        {{v2.authName}}
                                    </el-tag>
                                </el-col>
                                <el-col :span="20">
                                    <el-tag closable type="info" v-for="v3 in v2.children" :key="v3.id" @close="delAccreditV3(scope.row,v3.id)">
                                        {{v3.authName}}
                                    </el-tag>
                                </el-col>
                            </el-row>
                        </el-col>
                    </el-row>
                    <el-row v-if='scope.row.children.length===0'>没有权限 请添加权限</el-row>
                </template>
            </el-table-column>
            <el-table-column type="index" width="50">
            </el-table-column>
            <el-table-column prop="roleName" label="角色名称" width="280">
            </el-table-column>
            <el-table-column prop="roleDesc" label="描述" width="280">
            </el-table-column>
            <el-table-column label="操作">
                <template slot-scope="scope">
                    <el-tooltip content="授权" placement="top">
                        <el-button type="warning" icon="el-icon-share" plain @click="accreditroles(scope.row)"></el-button>
                    </el-tooltip>
                    <el-tooltip content="编辑" placement="top">
                        <el-button type="success" icon="el-icon-edit" plain @click="editroles(scope.row)"></el-button>
                    </el-tooltip>
                    <el-tooltip content="删除" placement="top">
                        <el-button type="danger" icon="el-icon-delete" plain @click="delroles(scope.row.id)"></el-button>
                    </el-tooltip>
                </template>
            </el-table-column>
        </el-table>
        <!-- 点击添加角色弹出层 -->
        <el-dialog title="添加角色" :visible.sync="addDialogFormVisible" @close='closeAddDialog("rolesForm")'>
            <el-form :model="rolesForm" :rules="rules" ref='rolesForm' class="demo-ruleForm">
                <el-form-item label="角色名称" label-width="500" prop='roleName'>
                    <el-input v-model="rolesForm.roleName" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="角色描述" label-width="500" prop='roleDesc'>
                    <el-input v-model="rolesForm.roleDesc" auto-complete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="addDialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="addRoles('rolesForm')">确 定</el-button>
            </div>
        </el-dialog>
        <!-- 点击授权弹出层 -->
        <el-dialog title="授权角色" :visible.sync="accreditRolesDialog">
            <div style="height:500px;overflow:auto">
                <el-tree :data="treeList" show-checkbox node-key="id" :default-checked-keys="defaultids" ref="tree" :props="defaultProps" :default-expand-all='true'>
                </el-tree>
            </div>
            <div slot="footer" class="dialog-footer">
                <el-button @click="accreditRolesDialog = false">取 消</el-button>
                <el-button type="primary" @click="accreditRole">确 定</el-button>
            </div>
        </el-dialog>
        <!-- 点击编辑角色弹出层 -->
    <el-dialog title="编辑角色" :visible.sync="editDialogFormVisible">
            <el-form :model="editForm" :rules="rules" ref='editForm' class="demo-ruleForm">
                <el-form-item label="角色名称" label-width="300" prop='roleName'>
                    <el-input v-model="editForm.roleName" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="角色描述" label-width="300" prop='roleDesc'>
                    <el-input v-model="editForm.roleDesc" auto-complete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="editDialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="editRoles('editForm')">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { getAllroles, addroles, delRoles, getALRrights, accreditRoles, delRolesAccredit, editRoles } from '@/api/index.js'
export default {
  data () {
    return {
      rolesId: '',
      rolesForm: {},
      editForm: {
        id: '',
        roleName: '',
        roleDesc: ''
      },
      addDialogFormVisible: false,
      accreditRolesDialog: false,
      editDialogFormVisible: false,
      rolesList: [
        {
          id: '',
          roleName: '',
          roleDesc: ''
        }
      ],
      rules: {
        roleName: [
          { required: true, message: '请输入角色名称', trigger: 'blur' },
          { min: 1, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
        ],
        roleDesc: [
          { }
        ]
      },
      treeList: [],
      defaultids: [],
      defaultProps: {
        children: 'children',
        label: 'authName'
      }
    }
  },
  mounted () {
    // 角色列表
    this.getAllRolesData()
  },
  methods: {
    // 关闭弹出层
    closeAddDialog (rolesForm) {
      this.$refs[rolesForm].resetFields()
    },
    // 编辑提交角色
    editRoles (editForm) {
      this.$refs[editForm].validate(valid => {
        if (valid) {
          editRoles(this.editForm).then(res => {
            console.log(res)
            if (res.meta.status === 200) {
              // 关闭弹出层
              this.editDialogFormVisible = false
              // 弹出提示框
              this.$message.success(res.meta.msg)
              // 刷新页面
              this.getAllRolesData()
            } else {
              this.$message.error(res.meta.msg)
            }
          })
        } else {
          this.$message.error('表单验证不成功')
          return false
        }
      })
    },
    //   点击编辑
    editroles (row) {
      this.editDialogFormVisible = true
      this.editForm.roleName = row.roleName
      this.editForm.id = row.id
      this.editForm.roleDesc = row.roleDesc
    },
    // 删除角色权限
    delAccreditV1 (row, rightId) {
      this.delAccredit(row, rightId)
    },
    delAccreditV2 (row, rightId) {
      this.delAccredit(row, rightId)
    },
    delAccreditV3 (row, rightId) {
      this.delAccredit(row, rightId)
    },
    // 封装删除角色权限
    delAccredit (row, rightId) {
      delRolesAccredit(row.id, rightId).then(res => {
        if (res.meta.status === 200) {
          // 刷新页面
          row.children = res.data
        } else {
          this.$message.error(res.meta.msg)
        }
      })
    },
    // 角色授权
    accreditRole () {
      // 获取勾选的数据
      var tempArr = this.$refs.tree.getCheckedNodes().map(val => {
        return val.id + ',' + val.pid
      })
      var temp = tempArr.join(',').split(',')
      //   ES6数组去重new Set() 对象转数组Array.from()
      var rids = Array.from(new Set(temp)).join()
      // 发送请求
      accreditRoles(this.rolesId, rids).then(res => {
        if (res.meta.status === 200) {
          // 弹出提示框
          this.$message.success(res.meta.msg)
          //   关闭弹出层
          this.accreditRolesDialog = false
          //   this.$refs.tree.setCheckedKeys([])
          // 重新加载页面
          this.getAllRolesData()
        } else {
          this.$message.error(res.meta.msg)
        }
      })
      // this.defaultids.length = 0
    //   this.$refs.tree.setCheckedKeys([])
    },
    // 所有权限列表
    accreditroles (row) {
      this.defaultids.length = 0
      this.rolesId = row.id
      this.accreditRolesDialog = true
      getALRrights('tree').then(res => {
        if (res.meta.status === 200) {
          this.treeList = res.data
          //   遍历最后的children的id 赋值给显示默认勾选的数组
          if (row.children.length > 0) {
            row.children.forEach(v1 => {
              if (v1.children.length > 0) {
                v1.children.forEach(v2 => {
                  if (v2.children.length > 0) {
                    v2.children.forEach(v3 => {
                      this.defaultids.push(v3.id)
                    })
                  }
                })
              }
            })
          }
        } else {
          this.$message.error(res.meta.msg)
        }
      })
    },
    // 删除角色
    delroles (id) {
      this.$confirm(`此操作将永久删除id为${id}的文件, 是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          delRoles(id).then(res => {
            if (res.meta.status === 200) {
              // 弹出提示框
              this.$message.success(res.meta.msg)
              // 重新加载页面
              this.getAllRolesData()
            } else {
              this.$message.error(res.meta.msg)
            }
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    //   添加角色
    addRoles (rolesForm) {
      this.$refs[rolesForm].validate(valid => {
        if (valid) {
          addroles(this.rolesForm).then(res => {
            if (res.meta.status === 201) {
              // 关闭弹出层
              this.addDialogFormVisible = false
              // 弹出提示框
              this.$message.success(res.meta.msg)
              // 刷新页面
              this.getAllRolesData()
              // 清空原来添加的类容
              // this.$refs[rolesForm].resetFields()
            } else {
              this.$message.error(res.meta.msg)
            }
          })
        } else {
          this.$message.error('表单验证不成功')
          return false
        }
      })
    },

    // 角色列表
    getAllRolesData () {
      getAllroles().then(res => {
        console.log(res)
        this.rolesList = res.data
      })
    }
  }
}
</script>

<style lang="scss" scoped>
/* 覆盖element导航菜单的样式 */
.el-breadcrumb {
  background-color: #f5f5f5;
  height: 45px;
  font-size: 15px;
  padding-left: 10px;
  line-height: 45px;
  margin-bottom: 15px;
}
.el-tag {
  margin: 5px;
}
</style>
